package util;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

import dao.Usertemp;

public class LoginCheckTag extends TagSupport
{
	@Override
	public int doEndTag() throws JspException {
		HttpSession session=pageContext.getSession();
		Usertemp user = (Usertemp)session.getAttribute("SESSION_USERINFO");
		if(null==user)
		{
			//response.sendRedirect(request.getContextPath()+"/login.jsp" ); 
			//sendRedirect与RequestDispatcher不仅在路径设置上不同，而且前者不能返回信息(跟为安全)
			ServletRequest request=pageContext.getRequest();
			ServletResponse response=pageContext.getResponse();
			RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");
			request.setAttribute("message", "illegal to access");
			try {
				rd.forward(request, response);
			}
			catch (IOException e)
			{
				throw new JspException(e);
			}
			catch (ServletException e)
			{
				throw new JspException(e);
			}
			return (SKIP_PAGE);
		}
		return (EVAL_PAGE);
	}

	@Override
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}
}

